package com.lfk.justweengine.utils.quad;

import ohos.agp.utils.RectFloat;
import org.junit.Assert;
import org.junit.Test;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class QuadTreeOhosTest {

    @Test
    public void testClear() {
        RectFloat rectFloat = new RectFloat(0, 0, 1, 2);
        QuadTree quadTree = new QuadTree(1, rectFloat);
        RectFloat rectFloat2 = new RectFloat(0,100,100,124);
        quadTree.insert(rectFloat2);
        quadTree.clear();
        try {
            Field f = quadTree.getClass().getDeclaredField("objects");
            f.setAccessible(true);
            List<RectFloat> objects = (List<RectFloat>) f.get(quadTree);
            Assert.assertTrue(objects.isEmpty());
        } catch (NoSuchFieldException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testInsert() throws Exception {
        RectFloat rectFloat = new RectFloat(0, 0, 1, 2);
        QuadTree quadTree = new QuadTree(1, rectFloat);
        RectFloat rectFloat2 = new RectFloat(0,100,100,124);
        quadTree.insert(rectFloat2);
        try {
            Field f = quadTree.getClass().getDeclaredField("objects");
            f.setAccessible(true);
            List<RectFloat> objects = (List<RectFloat>) f.get(quadTree);

            Assert.assertFalse(objects.isEmpty());

        } catch (NoSuchFieldException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testRetrieve() {
        RectFloat rectFloat = new RectFloat(0, 0, 1, 2);
        QuadTree quadTree = new QuadTree(1, rectFloat);
        RectFloat rectFloat2 = new RectFloat(0,100,100,124);
        List<RectFloat> list = new ArrayList<>();
        list.add(rectFloat2);
        List<List<RectFloat>> returnObjects = new ArrayList<>();
        returnObjects.add(list);
        List<List<RectFloat>> aa = quadTree.retrieve(returnObjects,rectFloat2);
        Assert.assertFalse(aa.isEmpty());
    }
}